/**
 * 
 */
package com.zd.common.common.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import org.springframework.web.multipart.MultipartFile;

import com.zd.common.common.utils.StringUtils;

/**
 * @author lishi
 * @date 2018年11月24日 下午1:17:54
 * 
 * 文件操作工具类
 */
public class FileUtils {

	public static String writeFile(String path, MultipartFile uploadFile) throws Exception {
		String originalFilename = uploadFile.getOriginalFilename();
		String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
		
		 File file = new File(path + "uploadFile");
	        if(!file.exists()){
	            file.mkdirs();
	        }
		String fileName = "/uploadFile/" + StringUtils.getRandomWithTimestamp(20) + "." + suffix;
		String pathName = path + fileName;
		BufferedOutputStream buff = new BufferedOutputStream(new FileOutputStream(new File(pathName)));
		buff.write(uploadFile.getBytes());
		buff.close();
		return fileName;
	}
}
